package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.UmsAdminRoleRelation;
import com.itheima.domain.UmsMenu;
import com.itheima.domain.UmsRoleMenuRelation;
import com.itheima.mapper.UmsMenuMapper;
import com.itheima.service.UmsAdminRoleRelationService;
import com.itheima.service.UmsMenuService;
import com.itheima.service.UmsRoleMenuRelationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 后台菜单表 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class UmsMenuServiceImpl extends ServiceImpl<UmsMenuMapper, UmsMenu> implements UmsMenuService {

    @Autowired
    private UmsMenuMapper umsMenuMapper;


    @Autowired
    UmsAdminRoleRelationService umsAdminRoleRelationService;
    @Autowired
    UmsRoleMenuRelationService umsRoleMenuRelationService;


    @Override
    public Page<UmsMenu> getListByPage(Integer pageNum, Integer pageSize, Integer parentId) {
        Page<UmsMenu> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsMenu::getParentId,parentId).orderByAsc(UmsMenu::getSort);
        return umsMenuMapper.selectPage(page,lqw);
    }

    @Override
    public UmsMenu getById(Integer id) {
        return umsMenuMapper.selectById(id);
    }

    @Override
    public Integer update(UmsMenu umsMenu) {
        return umsMenuMapper.updateById(umsMenu);
    }

    @Override
    public Integer updateHiddenById(Integer id, Integer hidden) {

        return umsMenuMapper.updateHiddenById(id,hidden);
    }

    @Override
    public Integer delete(Integer id) {
       return umsMenuMapper.deleteById(id);
    }

    @Override
    public Integer create(UmsMenu umsMenu) {
      return umsMenuMapper.insert(umsMenu);
    }

    @Override
    public List<UmsMenu> getByIds(List<Long> menuIds) {
       return umsMenuMapper.selectBatchIds(menuIds);
    }

    @Override
    public List<UmsMenu> getAll() {
        return umsMenuMapper.getAll();
    }


    @Override
    public List<UmsMenu> getMenuByUserId(Long id) {
        LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsAdminRoleRelation::getAdminId,id);
        UmsAdminRoleRelation umsAdminRoleRelation = umsAdminRoleRelationService.getOne(queryWrapper);
        //查询当前用户的权限
        LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(UmsRoleMenuRelation::getRoleId,umsAdminRoleRelation.getRoleId());
        List<UmsRoleMenuRelation> list = umsRoleMenuRelationService.list(queryWrapper1);
        List<UmsMenu> menus = new ArrayList<>();
        for (UmsRoleMenuRelation umsRoleMenuRelation : list) {
            UmsMenu menu = this.getById(umsRoleMenuRelation.getMenuId());
            menus.add(menu);
        }
        return menus;
    }

}
